//
//  SystemMessageVC.m
//  sensen_ios
//
//  Created by 小贝 on 16/3/22.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "SystemMessageVC.h"
#import "SystemMessageCell.h"
#import "SystemModel.h"
#import "WebViewController.h"
@interface SystemMessageVC ()<UITableViewDelegate,UITableViewDataSource>
// 视图
@property (nonatomic, strong) UITableView *tableView; // 表视图
@property (nonatomic, strong) UILabel *emptyView; // 没有信息时候的视图
@end

@implementation SystemMessageVC
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"系统通知";
    self.view.backgroundColor = [UIColor whiteColor];
    [self createLeftBarButtonItemWithImagename:nil];
    [self _createViews]; // 创建视图
    [self _initData]; // 获取数据
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    // 隐藏标签栏
    MainTabBarController *main = (MainTabBarController *)self.navigationController.tabBarController;
    [main hideTabBar];
    // 设置导航栏颜色
      [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:SS_NAVI_ORANGE_COLOR] forBarMetrics:UIBarMetricsDefault];
    if (_messagesAry.count == 0 || _messagesAry == nil){
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:SS_NAVI_BLUE_COLOR] forBarMetrics:UIBarMetricsDefault];
    }else{
        
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:SS_NAVI_ORANGE_COLOR] forBarMetrics:UIBarMetricsDefault];
    }
}
- (void)viewWillDisappear:(BOOL)animated {
    // 显示标签栏
    MainTabBarController *main = (MainTabBarController *)self.navigationController.tabBarController;
    [main showTabBar];
    
}

#pragma mark - 获取数据
- (void)_initData {
        // 没有数据的时候就隐藏表视图
        if (_messagesAry.count == 0 || _messagesAry == nil) {
            _tableView.hidden = YES;
        } else {
            _emptyView.hidden = YES;
            
            // 刷新表视图
            [_tableView reloadData];
        }
}
#pragma mark - 创建视图
- (void)_createViews {
    
    [self _createEmptyViewView]; // 没有信息时候的视图
    [self _initTableView]; // 创建表视图
}
// 创建表视图
- (void)_initTableView {
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = Gray_COLOR;
    [self.view addSubview:_tableView];
    
    // 注册单元格
    UINib *nib = [UINib nibWithNibName:@"SystemMessageCell" bundle:nil];
    [_tableView registerNib:nib forCellReuseIdentifier:@"SystemMessageCell"];
}

// 没有信息时候的视图
- (void)_createEmptyViewView {
 
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat width = 100;
    CGFloat height = 20;
    _emptyView = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    _emptyView.font = FONT(15);
    _emptyView.text = @"暂无消息通知";
    _emptyView.textColor = [UIColor darkGrayColor];
    _emptyView.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:_emptyView];
    
    _emptyView.center = self.view.center;
}

#pragma mark - UITableViewDataSource UITableViewDelegate
// 设置组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _messagesAry.count;
}
// 设置单元格属性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SystemMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SystemMessageCell"];
    SystemModel *sysModel = self.messagesAry[indexPath.row];
    cell.systemModel = sysModel;
    // 设置单元格不可点击
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.lookDetail.tag = indexPath.row;
    [cell.lookDetail addTarget:self action:@selector(changeVC:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
// 设置单元格行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 140;
}
// 设置组头高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 1;
}
//设置组尾高
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.000001;
}

// 单元格点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    MyMessageVC *myVC = [[MyMessageVC alloc] init];
//    SystemModel *sysModel = self.messagesAry[indexPath.row];
//    myVC.content = sysModel.content;
//    [self.navigationController pushViewController:myVC animated:YES];
    SystemModel *model = self.messagesAry[indexPath.row];
    // 跳转web页面
    UserModel *user = [CommonCache getUserInfo];
    NSString *url = [NSString stringWithFormat:@"%@/index/systemMessage?id=%@&psw=%@&uid=%@",WEB_BASE_URL,model.msg_id,user.password,user.uid];
    [self jumpWebView:url and:@"我的消息"];
    

}

#pragma mark---点击查看详情
- (void)changeVC:(UIButton *)sender{
//    MyMessageVC *myVC = [[MyMessageVC alloc] init];
    UIButton *lookBtn = (UIButton *)sender;
    SystemModel *model = self.messagesAry[lookBtn.tag];
    // 跳转web页面
    UserModel *user = [CommonCache getUserInfo];
    NSString *url = [NSString stringWithFormat:@"%@/index/systemMessage?id=%@&psw=%@&uid=%@",WEB_BASE_URL,model.msg_id,user.password,user.uid];
    [self jumpWebView:url and:@"我的消息"];//    myVC.content = sysModel.content;
//    [self.navigationController pushViewController:myVC animated:YES];
}
#pragma mark --跳转网页
- (void)jumpWebView:(NSString *) url  and:(NSString *) titleStr{
    WebViewController *webVC = [[WebViewController alloc] initWithUrlString:url];
    webVC.titleString = titleStr;
    [self.navigationController pushViewController:webVC animated:YES];
}

#pragma mark------------
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
